import { Module } from '@nestjs/common';
import { UploadController } from './upload.controller';
import { UploadService } from './upload.service';

import { MulterModule } from '@nestjs/platform-express';
import dayjs = require('dayjs');
import { diskStorage } from 'multer';
import * as nuid from 'nuid';
import { isExist } from '@/utils/default-upload';
@Module({
  imports: [
    MulterModule.register({
      storage: diskStorage({
        //自定义路径
        destination(req, file, callback) {
          console.log(req, file);
          const fileDir = `./fileUpload/${dayjs().format('YYYY-MM-DD')}`;
          try {
            isExist(fileDir);
          } catch (error) {
            callback(error, fileDir);
          }
          callback(null, fileDir);
        },
        // destination: path.resolve(__),
        filename: (req, file, cb) => {
          // 自定义文件名
          const filename = `${nuid.next()}.${file.mimetype.split('/')[1]}`;
          return cb(null, filename);
          // return cb(null, file.originalname);
        },
      }),
    }),
  ],
  controllers: [UploadController],
  providers: [UploadService],
})
export class UploadModule {}
